package com.sgb.kjwl.view.widget.manage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.sgb.kjwl.R;
import com.sgb.link.utils.SizeUtils;

public class GradientBgTextView extends AppCompatTextView {

    private Context mContext;
    private Paint mPaint;
    private Path path = new Path();
    private LinearGradient linearGradient;
    private RectF rect;
    private int mStartColor, mEndColor;
    private int mRadius;
    private int mColors[];
    private float[] radii;

    public GradientBgTextView(Context context) {
        this(context, null);
    }

    public GradientBgTextView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientBgTextView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GradientBgTextView);
        mStartColor = typedArray.getColor(R.styleable.GradientBgTextView_startColor, getResources().getColor(R.color.color_gradient));
        mEndColor = typedArray.getColor(R.styleable.GradientBgTextView_endColor, getResources().getColor(R.color.color_main));
        mRadius = typedArray.getDimensionPixelOffset(R.styleable.GradientBgTextView_bgRadius, SizeUtils.dp2px(2));
       // radii = new float[]{mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius};
        typedArray.recycle();
        init();
        setBackgroundResource(0);
    }

    public void init(){
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        rect = new RectF();
        mColors = new int[]{mStartColor, mEndColor};
    }

    public void setRadius(int radius) {
        this.mRadius = radius;
        //radii = new float[]{mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius};
        requestLayout();
        invalidate();
    }

    public void setColors(@ColorRes int startColor, @ColorRes int endColor){
        mStartColor = getResources().getColor(startColor);
        mEndColor = getResources().getColor(endColor);
        mColors = new int[]{mStartColor, mEndColor};
        invalidate();
        requestLayout();
    }

    public void setEndColors(@ColorInt int startColor, @ColorInt int endColor){
        mColors = new int[]{startColor, endColor};
        invalidate();
        requestLayout();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rect.set(0, 0, w, h);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        linearGradient = new LinearGradient(0, 0, getWidth(),0, mColors, null, Shader.TileMode.CLAMP);

        mPaint.setShader(linearGradient);
        path.addRoundRect(rect, mRadius, mRadius, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
        canvas.save();
        canvas.restore();

        super.onDraw(canvas);
    }
}
